<template>
  <div class="edit-box">
    <!-- 我的频道 -->
    <van-cell-group>
      <van-cell class="common-cell">
        <span slot="title" class="left-title">我的频道</span>
        <van-button class="edit-btn" type="default" size="mini" round color="#f85959" plain @click="isEdit = !isEdit">
          {{isEdit ? '完成' : '编辑'}}
        </van-button>
      </van-cell>
    </van-cell-group>
    <van-grid class="common-gird my-grid" :gutter="10" :border='false'>
      <van-grid-item :class="{active: index === active}" v-for="(item,index) in channel" :key="item.id"
        :icon="isEdit && index !== 0 ? 'close' : ''" :text="item.name" @click="clickMyChannel(index,item.id)" />
    </van-grid>
    <!-- 推荐频道 -->
    <van-cell-group>
      <van-cell class="common-cell">
        <span slot="title" class="left-title">频道推荐</span>
      </van-cell>
    </van-cell-group>
    <van-grid class="common-gird recommend-grid" :gutter="10" :border='false' direction="horizontal">
      <van-grid-item v-for="item in recommendChannel" :key="item.id" icon="plus" :text="item.name"
        @click="addChannel(item)" />
    </van-grid>
  </div>
</template>

<script>
import { setItem } from '@/utils/storage.js'
import { mapState } from 'vuex'
import { getAllChannel, addUserChannel, delUserChannel } from '@/api/channel.js'
const TT_CHANNEL = 'tt_channel'
export default {
  props: {
    // 父组件频道列表
    channel: {
      type: Array,
      require: true
    },
    // 父组件选中频道
    active: {
      type: Number,
      require: true
    }
  },
  data () {
    return {
      // 所有频道数据
      allChannelList: [],
      // 我都频道是否处于编辑状态
      isEdit: false
    }
  },
  methods: {
    // 加载全部频道列表
    async loadChannel () {
      try {
        const { data: res } = await getAllChannel()
        this.allChannelList = res.data.channels
      } catch (err) {
        this.$toast('获取全部频道列表失败')
      }
    },
    // 点击推荐频道标签
    async addChannel (item) {
      this.channel.push(item)
      if (this.user) {
        // 登录,存储到数据库
        try {
          await addUserChannel({
            id: item.id,
            seq: this.channel.length
          })
          this.$toast.success('添加成功')
        } catch (err) {
          this.$toast.success('添加失败')
        }
      } else {
        // 未登录,存储到本地
        setItem(TT_CHANNEL, this.channel)
      }
    },
    // 点击我的频道标签
    clickMyChannel (index, id) {
      if (this.isEdit) {
        // 编辑状态下删除
        if (index === 0) return
        if (index < this.active) {
          this.$emit('sendData', {
            close: true,
            active: this.active - 1
          })
        }
        this.channel.splice(index, 1)
        // 数据持久化
        this.deleteChannelStorage(id)
      } else {
        // 非编辑状态下跳转
        this.$emit('sendData', {
          close: false,
          active: index
        })
      }
    },
    // 删除数据持久化
    deleteChannelStorage (id) {
      if (this.user) {
        // 登录
        delUserChannel(id)
      } else {
        // 未登录
        setItem(TT_CHANNEL, this.channel)
      }
    }
  },
  computed: {
    // 推荐频道列表
    recommendChannel () {
      // 把我的频道中不存在的放到推荐中
      return this.allChannelList.filter(item => {
        return !this.channel.find(val => item.id === val.id)
      })
    },
    ...mapState(['user'])
  },
  watch: {},
  filters: {},
  components: {},
  created () {
    this.loadChannel()
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.edit-box {
  padding-top: 80px;
  // 公共单元格样式
  .common-cell {
    align-items: center;
    .left-title {
      font-size: 32px;
      color: #333;
    }
    .edit-btn {
      padding-top: 5px;
      width: 100px;
      height: 44px;
    }
  }
  // 公共宫格样式
  /deep/ .common-gird {
    .van-grid-item {
      height: 86px;
      .van-grid-item__content {
        border-radius: 6px;
        background-color: #f4f5f6;
      }
    }
  }
  // 推荐频道宫格样式
  /deep/ .recommend-grid {
    .van-icon {
      font-size: 24px;
    }
  }
  // 我的频道宫格样式
  /deep/ .my-grid {
    .van-grid-item__icon + .van-grid-item__text {
      margin-top: 0;
    }
    .van-icon-close {
      position: absolute;
      top: -10px;
      right: 5px;
      font-size: 34px;
    }
    // 选中文字高亮
    .active {
      .van-grid-item__text {
        color: #f85959;
      }
    }
  }
}
.van-hairline--top-bottom {
  &::after {
    border: none;
  }
}
</style>
